package net.towee.model.group.impl;

import java.util.Date;
import java.util.LinkedList;

import net.towee.model.ProfileArea;
import net.towee.model.group.GroupIdentity;
import net.towee.model.group.GroupOpenness;
import net.towee.model.group.GroupProfile;
import net.towee.model.impl.ProfileContainerImpl;
import net.towee.server.persistence.impl.MongoDbStore;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Indexed;
import com.google.code.morphia.annotations.PostLoad;
import com.google.code.morphia.annotations.Reference;

/**
 * Group profile implementation
 * @author c58
 */
@Entity(MongoDbStore.GROUP_PROFILE_COLLECTION)
public class GroupProfileImpl extends ProfileContainerImpl implements GroupProfile {
	@Id private String id;
	@Reference private GroupIdentity identity;
	
	// Openness of group
	@Indexed private GroupOpenness openness;
	
	// Date of group creation
	private Date creationDate;
	
	// Number of users
	private int users = 0;
	
	// Static profile
	@Indexed private String homeUrl = "";
	@Indexed private String desc = "";
	
	public GroupProfileImpl() {}
	public GroupProfileImpl(GroupIdentity id, GroupOpenness openness) {
		this.id = id.getId();
		this.identity = id;
		
		this.openness = openness;
		this.users = 0;
		this.creationDate = new Date();
		this.profileAreas = new LinkedList<ProfileArea>();
	}
	
	@PostLoad
	private void createEmptyArrays() {
		if(profileAreas == null)
			profileAreas = new LinkedList<ProfileArea>();
	}
	
	@Override
	public String getId() {
		return id;
	}
	
	@Override
	public GroupOpenness getOpenness() {
		return openness;
	}

	@Override
	public void setOpenness(GroupOpenness openness) {
		this.openness = openness;
	}
	
	@Override
	public Date getCreationDate() {
		return creationDate;
	}
	
	@Override
	public int getNumberOfUsers() {
		return users;
	}
	
	@Override
	public void incrementUsers() {
		users++;
	}
	
	@Override
	public String getHomeUrl() {
		return homeUrl;
	}

	@Override
	public String getDescription() {
		return desc;
	}

	@Override
	public void setHomeUrl(String url) {
		this.homeUrl = url;
	}

	@Override
	public void setDescription(String desc) {
		this.desc = desc;
	}
	@Override
	public GroupIdentity getIdentity() {
		return identity;
	}
	
	@Override
	public void setId(String string) {
		this.id = string;
	}
}
